using Microsoft.EntityFrameworkCore;
using DaoHang.Models;

namespace DaoHang.Data
{
    /// <summary>
    /// 应用程序数据库上下文类，用于配置Entity Framework Core
    /// </summary>
    public class ApplicationDbContext : DbContext
    {
        /// <summary>
        /// 构造函数，接收数据库配置选项
        /// </summary>
        /// <param name="options">数据库配置选项</param>
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        /// <summary>
        /// 分类数据集
        /// </summary>
        public DbSet<Category> Categories { get; set; }

        /// <summary>
        /// 网站数据集
        /// </summary>
        public DbSet<Website> Websites { get; set; }

        /// <summary>
        /// 用户数据集
        /// </summary>
        public DbSet<User> Users { get; set; }

        /// <summary>
        /// 网站全局设置数据集
        /// </summary>
        public DbSet<SiteSettings> SiteSettings { get; set; }

        /// <summary>
        /// 系统日志数据集
        /// </summary>
        public DbSet<SystemLog> SystemLogs { get; set; }

        /// <summary>
        /// AI配置数据集
        /// </summary>
        public DbSet<AIConfig> AIConfigs { get; set; }

        /// <summary>
        /// 日志条目数据集
        /// </summary>
        public DbSet<LogEntry> LogEntries { get; set; }

        /// <summary>
        /// 配置实体模型关系和约束
        /// </summary>
        /// <param name="modelBuilder">模型构建器</param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置分类实体
            modelBuilder.Entity<Category>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(100);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.HasIndex(e => e.Name).IsUnique();
            });

            // 配置网站实体
            modelBuilder.Entity<Website>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Name).IsRequired().HasMaxLength(200);
                entity.Property(e => e.Url).IsRequired().HasMaxLength(500);
                entity.Property(e => e.Description).HasMaxLength(1000);
                entity.Property(e => e.IconUrl).HasMaxLength(500);
                
                // 配置外键关系
                entity.HasOne(e => e.Category)
                      .WithMany(c => c.Websites)
                      .HasForeignKey(e => e.CategoryId)
                      .OnDelete(DeleteBehavior.Cascade);
            });

            // 配置用户实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
                entity.Property(e => e.PasswordHash).IsRequired();
                entity.Property(e => e.Email).HasMaxLength(100);
                entity.HasIndex(e => e.Username).IsUnique();
            });

            // 配置网站设置实体
            modelBuilder.Entity<SiteSettings>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.IcpNumber).HasMaxLength(50);
                entity.Property(e => e.PoliceNumber).HasMaxLength(50);
                entity.Property(e => e.ApiKey).HasMaxLength(500);
            });

            // 配置系统日志实体
            modelBuilder.Entity<SystemLog>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Level).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Category).IsRequired().HasMaxLength(50);
                entity.Property(e => e.Message).IsRequired();
                entity.Property(e => e.UserId).HasMaxLength(100);
                entity.Property(e => e.IpAddress).HasMaxLength(45);
                entity.Property(e => e.UserAgent).HasMaxLength(500);
                entity.Property(e => e.RequestPath).HasMaxLength(500);
                entity.HasIndex(e => e.CreateTime);
                entity.HasIndex(e => e.Level);
                entity.HasIndex(e => e.Category);
            });

            // 配置AI配置实体
            modelBuilder.Entity<AIConfig>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.ApiKey).IsRequired().HasMaxLength(500);
                entity.Property(e => e.ApiUrl).IsRequired().HasMaxLength(500);
                entity.Property(e => e.ModelName).IsRequired().HasMaxLength(200);
                entity.Property(e => e.Provider).HasMaxLength(100);
                entity.Property(e => e.Description).HasMaxLength(500);
                entity.Property(e => e.TestErrorMessage).HasMaxLength(1000);
                entity.HasIndex(e => e.IsEnabled);
                entity.HasIndex(e => e.UpdateTime);
            });

            // 配置日志条目实体
            modelBuilder.Entity<LogEntry>(entity =>
            {
                entity.HasKey(e => e.Id);
                entity.Property(e => e.Level).IsRequired().HasMaxLength(20);
                entity.Property(e => e.Message).IsRequired();
                entity.Property(e => e.Logger).HasMaxLength(255);
                entity.Property(e => e.IpAddress).HasMaxLength(45);
                entity.Property(e => e.UserAgent).HasMaxLength(500);
                entity.Property(e => e.RequestUrl).HasMaxLength(500);
                entity.Property(e => e.Username).HasMaxLength(100);
                entity.HasIndex(e => e.Timestamp);
                entity.HasIndex(e => e.Level);
            });
        }
    }
} 